using System;
using System.Collections;
using BitMango;
using UnityEngine;

public class GiftSystem : SingletonMonoBehaviour<GiftSystem>
{
	private void Start()
	{
		GiftSystem.gapTotalSeconds = 0.0;
		base.StartCoroutine(this.CoSetGiftTime());
	}

	private void Update()
	{
	}

	private IEnumerator CoSetGiftTime()
	{
		while (!Wallet.IsValid())
		{
			yield return new WaitForEndOfFrame();
		}
		for (;;)
		{
			if (Wallet.GetItemCount("freegift") < 1)
			{
				string text = PlayerPrefs.GetString("SAVE_GIFTTIME", "NONE");
				if (text.Equals("NONE"))
				{
					PlayerPrefs.SetString("SAVE_GIFTTIME", GiftSystem.GetServerTime().ToString());
					PlayerPrefs.Save();
					text = GiftSystem.GetServerTime().ToString();
				}
				DateTime d = DateTime.Parse(text).AddMinutes((double)GiftSystem.chargeMinutesTime);
				TimeSpan timeSpan = d - GiftSystem.GetServerTime();
				if (timeSpan.TotalSeconds <= 0.0)
				{
					break;
				}
				GiftSystem.giftTime = string.Format("{0:00}:{1:00}", timeSpan.Minutes, timeSpan.Seconds);
			}
			else if (!PlayerPrefs.GetString("SAVE_GIFTTIME").Equals("NONE"))
			{
				goto Block_4;
			}
			yield return new WaitForSeconds(1f);
		}
		Wallet.Gain("freegift", 1, null);
		Wallet.Save();
		PlayerPrefs.SetString("SAVE_GIFTTIME", "NONE");
		PlayerPrefs.Save();
		yield break;
		Block_4:
		PlayerPrefs.SetString("SAVE_GIFTTIME", "NONE");
		PlayerPrefs.Save();
		yield break;
		yield break;
	}

	public static string GetGiftTime()
	{
		return (!GiftSystem.IsGift()) ? GiftSystem.giftTime : string.Empty;
	}

	public static bool IsGift()
	{
		return Wallet.GetItemCount("freegift") >= 1;
	}

	public static DateTime GetServerTime()
	{
		return DateTime.UtcNow.AddSeconds(GiftSystem.gapTotalSeconds);
	}

	public void UseGift()
	{
		GiftSystem.gapTotalSeconds = 0.0;
		Wallet.Use("freegift", 1, null);
		Wallet.Save();
		base.StartCoroutine(this.CoSetGiftTime());
	}

	private static int chargeMinutesTime = 5;

	private DateTime serverDate = DateTime.UtcNow;

	private static double gapTotalSeconds;

	private static string giftTime = string.Empty;
}
